/**
*@param {array} position  是一个二维数组 [[114,30],[114,40]]
*@param {array} attr   [
    {type:"string",key:"name",value:"黄河"},
    {type:"int",key:"length",value:1002}
]
*@param {object} service  {name,layerId}
*/
function addLine({ position, attr, service, docLayer }) {
  /* 1-1、几何 */
  var pointObj = new Array()
  /* [[114,30],[114,40],[120,40]] */
  for (let item of position) {
    pointObj.push(new Zondy.Object.Point2D(item[0], item[1]))
  }
  // pointObj[0] = new Zondy.Object.Point2D(114, 30);
  // pointObj[1] = new Zondy.Object.Point2D(120, 40);
  // pointObj[2] = new Zondy.Object.Point2D(120, 40);
  //构成折线的弧段
  var gArc = new Zondy.Object.Arc(pointObj)
  //构成线的折线
  var gAnyLine = new Zondy.Object.AnyLine([gArc])
  //设置线要素的几何信息
  var gline = new Zondy.Object.GLine(gAnyLine)
  //设置要素的几何信息
  var fGeom = new Zondy.Object.FeatureGeometry({ LinGeom: [gline] })
  /* 1-2、样式信息 */
  //设置添加线要素的图形参数信息
  var clineInfo = new Zondy.Object.CLineInfo({
    Color: 1,
    LinStyleID: 0,
    LinStyleID2: 22,
    LinWidth: 0.05,
    Xscale: 10,
    Yscale: 10,
  })
  //设置要素的图形参数信息
  var graphicInfo = new Zondy.Object.WebGraphicsInfo({
    InfoType: 2,
    LinInfo: clineInfo,
  })
  /* 1-3属性信息
  properties
   */
  /* value */
  var attValue = attr.map((item) => item.value)
  //创建一个线要素
  var newFeature = new Zondy.Object.Feature({
    fGeom: fGeom,
    GraphicInfo: graphicInfo,
    AttValue: attValue,
  })
  //设置要素为线要素
  newFeature.setFType(2)
  /* 2、创建要素集添加要素-设置字段映射 */
  //创建一个要素数据集
  var featureSet = new Zondy.Object.FeatureSet()

  //创建属性结构设置对象
  /* 全字段添加不用写映射 */
  var cAttStruct = new Zondy.Object.CAttStruct({
    /* key */
    FldName: attr.map((item) => item.key),
    FldNumber: attValue.length,
    /* type */
    FldType: attr.map((item) => item.type),
  })
  //设置要素数据集的树形结构
  featureSet.AttStruct = cAttStruct
  //将添加的线要素添加到属性数据集中
  featureSet.addFeature(newFeature)
  /* 3、调用服务添加要素集  */
  //创建一个地图编辑对象
  var editDocFeature = new Zondy.Service.EditDocFeature(
    service.name,
    service.layerId,
    {}
  )
  editDocFeature.add(featureSet, onLineSuccess(docLayer))
}
function onLineSuccess(docLayer) {
  return function (result) {
    if (result) {
      alert("添加线要素成功！")
      // docLayer.refresh()
    } else {
      alert("添加线要素失败！")
    }
  }
}

export { addLine }
